package com.tiancheng.trade.merchant.configuration;

import cn.dev33.satoken.config.SaTokenConfig;
import com.tiancheng.admintoken.config.AuthTokenConfig;
import com.tiancheng.trade.commom.web.auth.OpenAuthTokenConfig;
import com.tiancheng.trade.commom.web.auth.StpCustomKit;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@EnableConfigurationProperties(OpenAuthTokenConfig.class)
@AutoConfigureAfter({OpenAuthTokenConfig.class})
@Configuration
public class OpenApiSaTokenConfig {

    @Resource
    private OpenAuthTokenConfig openAuthTokenConfig;

    @PostConstruct
    public void setOpenApiSaTokenConfig() {
        if (openAuthTokenConfig.getOpenApiTokenConfig() != null) {
            OpenAuthTokenConfig.TokenConfig platformTokenConfig = openAuthTokenConfig.getOpenApiTokenConfig();
            SaTokenConfig saPlatformTokenConfig = new SaTokenConfig();
            saPlatformTokenConfig.setTokenName(platformTokenConfig.getTokenName());
            saPlatformTokenConfig.setIsConcurrent(platformTokenConfig.getIsConcurrent());
            saPlatformTokenConfig.setIsShare(platformTokenConfig.getIsShare());
            saPlatformTokenConfig.setJwtSecretKey(platformTokenConfig.getJwtSecretKey());
            saPlatformTokenConfig.setTokenPrefix(platformTokenConfig.getTokenPrefix());
            StpCustomKit.OPEN.setConfig(saPlatformTokenConfig);
        }
    }
}
